//! 已停止
const puppeteer = require("puppeteer");
const moment = require("moment");
const getCaptcha = require("../../../common/image");

const { createMobile, config } = require("../../../common/mobile");
const {
  info,
  saveFile,
  logging,
  timeout,
  reporter
} = require("../../../common/util");

const name = "a1";
const today = moment().format("YYYY-MM-DD");
const logdir = `./logs/${today}/au/a1/${info.deviceid}`;

const r = reporter(name);
const logger = logging(name, logdir);

(async () => {
  const browser = await puppeteer.launch(config);
  const page = await browser.newPage();

  const m = await createMobile(page);

  //sub status linstener
  m._page.on("response", response => {
    let url = response.url();
    let status = response.status();

    if (url.startsWith("http")) {
      logger.info(`${response.request().method()}`, status, url);
    }

    if (url.startsWith("https://services.dimoco.at/dcb/AT/mobilkom/redir")) {
      r.s("success");
    }

    if (url.startsWith("http://google.com")) {
      r.w("failure", url);
      m.close();
    }
  });

  //开始
  try {
    r.i("start");

    logger.info("start");

    await m.get(`http://offer.lpaosub.com/offer/track?of=1062`);
    // await m.get(`http://dat.abfunny.com/dm/at/lp?track=2863251`);

    //等待页面加载完成
    await m._page.waitForSelector("#agree", { timeout });
    await m._page.waitForSelector("#submit_btn_mobile", { timeout });

    //勾选
    await m.evaluate(() => {
      document.getElementById("agree").click();
    });

    r.i("page1 click to page2");

    await m.tapElement("button[id='submit_btn_mobile']");
    logger.info("page1 click");

    // 进入第2页
    // await m._page.waitForNavigation({ timeout });
    await m._page.waitForSelector("input[id='vasb-captcha-input']", {
      timeout
    });
    await m._page.waitForSelector(
      "button[class='button confirm button-A1-confirm']",
      { timeout }
    );

    const image = await m._page.$("#vasb-captcha-image");
    const captchaRes = await getCaptcha(m, image, today, info.deviceid, logger);
    logger.info("page2 recaptcha");
    // <button class="button confirm button-A1-confirm" type="submit" name="confirm" value="true"> Kaufen </button>
    // <input id="vasb-agb-input" class="checkbox" type="checkbox" name="agb" value="true">
    // <input id="vasb-fagg-input" class="checkbox" type="checkbox" name="fagg" value="true">
    // <input id="vasb-captcha-input" class="textfield" name="captcha" type="text" maxlength="5" size="5">
    await m.evaluate(() => {
      document.getElementById("vasb-captcha-input").value = captchaRes;
      document.getElementById("vasb-agb-input").click();
      document.getElementById("vasb-fagg-input").click();
    });

    saveFile(`${logdir}/captcha.html`, await m._page.content());
    logger.info("captcha page saved");

    await m.tapElement("button[class='button confirm button-A1-confirm']");

    // 等待跳转
    //await m._page.waitForNavigation({ timeout });
    logger.info("submit");
    logger.info("end");
    r.i("end");
  } catch (e) {
    saveFile(`${logdir}/error.html`, await m._page.content());
    r.e(e + "");
    logger.error(e);
  } finally {
    await m.close();
  }
  await browser.close();
})();
